package com.yunxun.utils;

import com.yunxun.entity.ArticleEntity;
import com.yunxun.entity.MessageEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class LoadingArticleUtil {
    public List<ArticleEntity> getContent(List<MessageEntity> messageEntities){
        DownWebPageUtil downWebPageUtil = new DownWebPageUtil();
        List<ArticleEntity> articleEntities = new ArrayList<>();
        ArticleEntity articleEntity;
        for(MessageEntity messageEntity:messageEntities){
           articleEntity = new ArticleEntity();
           String contentUrl = messageEntity.getTitleUrl();
           String htmlCode = downWebPageUtil.getHtmlCode(contentUrl,"utf-8");
           setArticleTitle(articleEntity,htmlCode);
           setArticleSummary(articleEntity,htmlCode);
           setArticleContent(articleEntity,htmlCode);
           articleEntities.add(articleEntity);
        }
        return articleEntities;
    }

    private void setArticleTitle(ArticleEntity article,String htmlCode){
        Document doc = Jsoup.parse(htmlCode);
        //通过选择器查找到h1标签所在位置
        Elements elements = doc.select("body").select("div.main-wrap").select("div.cnbeta-article-wrapper")
                .select("div.cnbeta-update").select("div.w1200").select("div.cnbeta-article").select("h1");
        article.setArticle_title(elements.get(0).text());
    }

    private void setArticleSummary(ArticleEntity article ,String htmlCode){
        //1.解析html
        Document doc = Jsoup.parse(htmlCode);
        // 2.使用选择器语法来定位文章概括所在区域
        Elements els =doc.select("body")
               .select(" div.main-wrap").select("div.cnbeta-article-wrapper").select("div.cnbeta-update")
                .select("div.w1200").select("div.cnbeta-article").select("div.cnbeta-article-body").select("div.article-summary");
        //3.找到文章概括中的唯一一张图片，并设置进summary_imgUrl属性
        String summary_imgUrl = els.select("img").get(0).attr("src");
        article.setSummary_imgUrl(summary_imgUrl);
       //4.过滤p标签，拿到所有p标签的内容,通过StringBuffer来拼接内容，然后设置进articlEntity的summary属性
        Elements pElements =els.select("p");
        StringBuffer sBuffer = new StringBuffer();
        Elements elments = null;
        for(Element el:pElements){
           sBuffer.append(el.text());
        }
        article.setArticle_summary(sBuffer.toString());
    }
    private void setArticleContent(ArticleEntity article,String htmlCode){
        //1.解析html
        Document doc = Jsoup.parse(htmlCode);
        // 2.使用选择器语法来查找元素
        Elements els =doc.select("#artibody");
        Elements contentEls =els.select("p");
        //3.过滤p标签，拿到所有p标签的内容,通过StringBuffer来拼接内容
        StringBuffer sBuffer = new StringBuffer();
        for(Element el:contentEls){
            sBuffer.append(el.select("img").attr("src"));
            sBuffer.append(el.text());
        }
        // 4.stringBuffer转成字符串后设置进article_content属性
        article.setArticle_content(sBuffer.toString());
    }
}
